package com.example.demo.controller;

import com.example.demo.domain.Account;
import com.example.demo.persistence.DigestUtil;
import com.example.demo.services.AccountService;
import com.example.demo.services.impl.NationalPolicyResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.awt.image.BufferedImage;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;


@Controller
@RestController
@CrossOrigin
public class AccountController {

    @Autowired
    private AccountService accountService;


    @RequestMapping(value = "/account/{username}",method = RequestMethod.GET)
    public NationalPolicyResult login(@PathVariable String username){
        Account result = accountService.getAccount(username);
        return new NationalPolicyResult().success(result);
    }

    @RequestMapping (value = "/account",method = RequestMethod.POST)
    public NationalPolicyResult loginView(@RequestBody String account1) throws JsonProcessingException {
        NationalPolicyResult result = null;
        NationalPolicyResult nationalPolicyResult = new NationalPolicyResult();
        System.out.println(account1);
        Account account0 = new ObjectMapper().readValue(account1, Account.class);
        System.out.println(account0);
        try {
            Account account = accountService.getAccount(account0.getUsername(), DigestUtil.getMD5(account0.getPassword().getBytes()));
            if(account != null){
                result = nationalPolicyResult.success(account);
            }else {
                result = nationalPolicyResult.build(1,"未找到用户！",null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

//    @RequestMapping(value = "/account/{username}/logOut",method = RequestMethod.GET)
//    public String logOut(Model model){
//        model.addAttribute("loginAccount",null);
//        model.addAttribute("cart",null);
//        model.addAttribute("myList",null);
//        return "catalog/main";
//    }

    @RequestMapping(value = "/account/{username}/update",method = RequestMethod.PUT)
    @ResponseBody
    public NationalPolicyResult updateAccount(@RequestBody String account0) throws SQLException, JsonProcessingException {
        Account account = new ObjectMapper().readValue(account0, Account.class);
        try {
            account.setPassword(DigestUtil.getMD5(account.getPassword().getBytes()));
            accountService.updateAccount(account);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return new NationalPolicyResult(Integer.getInteger("2"),"修改成功！",account);
    }

    @RequestMapping(value = "/account/register",method = RequestMethod.PUT)
    public NationalPolicyResult registerAccount(@RequestBody String account0) throws SQLException, JsonProcessingException {
        Account account = new ObjectMapper().readValue(account0, Account.class);
        try {
            account.setPassword(DigestUtil.getMD5(account.getPassword().getBytes()));
            accountService.insertAccount(account);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return new NationalPolicyResult(Integer.getInteger("2"),"注册成功！",account);
    }

}
